<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace app\admin\model;

use think\Model;
use think\Db;

/**
 * 车型MODEL
 * @package app\admin\model
 */
class MatorModel extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'e_mator';
	
	/**
     * 获取车型列表
     * @param int $page 当前分页
	 * @param int $export 是否导出
     * @param string $keyword 搜索关键字
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function getMatorList($input)
	{
		$query = Db::name($this->table)->where('mator_status','in','[0,1]');
		$page = isset($input['page']) ? $input['page'] : 1;
		$export = isset($input['export']) ? $input['export'] : 0;
		$order_field = isset($input['order_field']) ? $input['order_field'] : '';
		$order_value = isset($input['order_value']) ? $input['order_value'] : 'desc';
		if(isset($input['field_id']) && !empty($input['field_id'])){
			$query = $this->where('field_id',$input['field_id']);
		}
		if(isset($input['country_id']) && !empty($input['country_id'])){
			$query = $this->where('country_id',$input['country_id']);
		}
		if(isset($input['make_id']) && !empty($input['make_id'])){
			$query = $this->where('make_id',$input['make_id']);
		}
		if(isset($input['f_c_id']) && !empty($input['f_c_id'])){
			$query = $this->where('f_c_id',$input['f_c_id']);
		}
		if(isset($input['series_id']) && !empty($input['series_id'])){
			$query = $this->where('series_id',$input['series_id']);
		}
		if(isset($input['product_id']) && !empty($input['product_id'])){
			$where = array();
			$where['sku_status'] = 0;
			$where['product_id'] = $input['product_id'];
			$mator = Db::name('e_product_sku')->where($where)->select();
			$mator_id_arr = array();
			if(!empty($mator)){
				foreach($mator as $_m){
					array_push($mator_id_arr,$_m['mator_id']);
				}
				$mator_id_arr = array_unique($mator_id_arr);
				$query = $this->where('id','not in',$mator_id_arr);
			}
		}
		if(isset($input['mator_ids']) && !empty($input['mator_ids'])){
			$query = $this->where('id','in',$input['mator_ids']);
		}
		if(isset($input['keyword']) && !empty($input['keyword'])){
			$query = $this->where('mator_code|mator_name','like','%'.$input['keyword'].'%');
		}
		$order_arr = ['id'=>'desc'];
		if(!empty($order_field)){
			$order_arr = [$order_field=>$order_value];
		}
		if($export){
			$list = $query->order($order_arr)->select();
			return [
				'list'=> $list,
				'page'=>array()
			];
		}else{
			$list = $query->order($order_arr)->paginate(config('paginate.list_rows'));
			$page = $list->render();
			return [
				'list'=>$list->toArray(),
				'page'=>$page
			];
		}
	}
	/**
     * 获取车型列表
     * @param input $array 数组
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function getMators($input)
	{
		return Db::name($this->table)->where('mator_status','in','[0,1]')->where('id','in',$input['mator_ids'])->select();
	}
	
	/**
     * 获取车型关联产品
     * @param int $mator_id 车型id
     * @param string $keyword 搜索关键字
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function getProductByMator($where)
	{
		return Db::name('e_product_sku')->where($where)->select();
	}
	
	/**
     * 车型数据导入
     * @param array $data 车型数据
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function matorImport($data)
	{
		foreach($data as $excel){
			$f_c_code = $excel[0];
			$f_c_arr = explode('-',$f_c_code);
			$field = DB::table('e_field')->where('field_code',$f_c_arr[0])->find();
			$country = DB::table('e_country')->where('country_code',$f_c_arr[1])->find();
			if(!empty($field) && !empty($country)){
				$f_c_row = DB::table('e_field_country')->where(array('field_id'=>$field['id'],'country_id'=>$country['id']))->find();
				$f_c_d = array();
				$f_c_d['field_id'] = $field['id'];
				$f_c_d['country_id'] = $country['id'];
				$f_c_d['update_time'] = time();
				if(!empty($f_c_row)){
					$f_c_id = $f_c_row['id'];
					Db::table('e_field_country')->where('id', $f_c_id)->update($f_c_d);
				}else{
					$f_c_d['create_time'] = time();
					Db::name('e_field_country')->insert($f_c_d);
					$f_c_id = Db::name('e_field_country')->getLastInsID();
				}
				$m_code = $excel[1];
				$make_row = DB::table('e_make')->where('make_code',$m_code)->find();
				$m_d = array();
				$m_d['field_id'] = $field['id'];
				$m_d['country_id'] = $country['id'];
				$m_d['f_c_id'] = $f_c_id;
				$m_d['make_code'] = $m_code;
				$m_d['make_name'] = $excel[2];
				$m_d['update_time'] = time();
				if(!empty($make_row)){
					$make_id = $make_row['id'];
					Db::table('e_make')->where('id', $make_id)->update($m_d);
				}else{
					$m_d['create_time'] = time();
					Db::name('e_make')->insert($m_d);
					$make_id = Db::name('e_make')->getLastInsID();
				}
				$s_code = $excel[3];
				$series_row = DB::table('e_series')->where('series_code',$s_code)->find();
				$s_d = array();
				$s_d['field_id'] = $field['id'];
				$s_d['country_id'] = $country['id'];
				$s_d['make_id'] = $make_id;
				$s_d['f_c_id'] = $f_c_id;
				$s_d['series_code'] = $s_code;
				$s_d['series_name'] = $excel[4];
				$s_d['from_time'] = strtotime($excel[5]);
				$s_d['to_time'] = strtotime($excel[6]);
				$s_d['update_time'] = time();
				if(!empty($series_row)){
					$series_id = $series_row['id'];
					Db::table('e_series')->where('id', $series_id)->update($s_d);
				}else{
					$s_d['create_time'] = time();
					Db::name('e_series')->insert($s_d);
					$series_id = Db::name('e_series')->getLastInsID();
				}
				$mator_code = $excel[7];
				$mator_row = DB::table('e_mator')->where('mator_code',$mator_code)->find();
				$mator_data = array();
				$mator_data['field_id'] = $field['id'];
				$mator_data['country_id'] = $country['id'];
				$mator_data['make_id'] = $make_id;
				$mator_data['series_id'] = $series_id;
				$mator_data['f_c_id'] = $f_c_id;
				$mator_data['mator_code'] = $mator_code;
				$mator_data['mator_name'] = $excel[8];
				$mator_data['engine'] = $excel[9];
				$mator_data['volume'] = $excel[10];
				$mator_data['capacity'] = $excel[11];
				$mator_data['horse_power'] = $excel[12];
				$mator_data['m_type'] = $excel[13];
				$mator_data['m_year'] = $excel[14];
				$mator_data['update_time'] = time();
				if(!empty($mator_row)){
					$mator_id = $mator_row['id'];
					Db::table('e_mator')->where('id', $mator_id)->update($mator_data);
				}else{
					$mator_data['create_time'] = time();
					Db::name('e_mator')->insert($mator_data);
				}
			}else{
				continue;
			}
		}
		return true;
	}
}
